import java.util.Scanner;
public class test2 {
    public static int maxSubArray(int[] arr) {
        if (arr == null || arr.length == 0) {
            return 0; // 如果数组为空，可以根据需求返回0或抛出异常
        }
        int currentMax = arr[0];
        int globalMax = arr[0];
        for (int i = 1; i < arr.length; i++) {
            currentMax = Math.max(arr[i], currentMax + arr[i]);
            globalMax = Math.max(globalMax, currentMax);
        }
        return globalMax;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 1. 读取数组长度
        System.out.print("请输入数组的长度: ");
        int n = scanner.nextInt();
        // 2. 初始化数组并读取元素
        int[] arr = new int[n];
        System.out.println("请输入数组的 " + n + " 个整数元素，用空格隔开:");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        // 3. 计算最大子数组和
        int maxSum = maxSubArray(arr);
        // 4. 输出结果
        System.out.println("该数组的最大子数组和为: " + maxSum);
        scanner.close();
    }
}
